package com.jinsi.service.impl;

import com.jinsi.mapper.ScClassesMapper;
import com.jinsi.pojo.ScClasses;
import com.jinsi.pojo.TreeData;
import com.jinsi.service.ScClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ScClassesServiceImpl implements ScClassesService {
    @Autowired
    private ScClassesMapper mapper;

    @Override
    public List<TreeData> getTree(int pid) {
        List<ScClasses> list = mapper.getTree(pid);
        List<TreeData> tree = new ArrayList<>();
        for (ScClasses i: list) {
            TreeData treeData = new TreeData();
            treeData.setId(i.getClassesId());
            treeData.setTitle(i.getClassesName());
            treeData.setChildren(getTree(i.getClassesId()));
            tree.add(treeData);
        }
        return tree;
    }

    @Override
    public void addClassesTree(Integer pid, String title, String classesLevels) throws Exception {
        int num = mapper.addClassesTree(pid, title, classesLevels);
        if (num == 0){
            throw new Exception();
        }
    }

    @Override
    public void upClassesTree(Integer id, String title, String classesLevels) throws Exception {
        int num = mapper.upClassesTree(id, title, classesLevels);
        if (num == 0){
            throw new Exception();
        }
    }

    @Override
    public void deClassesTree(Integer id) throws Exception {
        int num = mapper.deClassesTree(id);
        if (num == 0){
            throw new Exception();
        }
    }
}
